using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Perovich.GameObjects
{
    public sealed class SpriteBatchCollection
    {
        Stack<SpriteBatch> reserveBatches = new Stack<SpriteBatch>();
        Stack<SpriteBatch> batches = new Stack<SpriteBatch>();
        public SpriteBatch CurrentBatch
        {
            get
            {
                return batches.Peek();
            }
        }

        GraphicsDevice device;

        public SpriteBatchCollection(GraphicsDevice device)
        {
            this.device = device;
            reserveBatches.Push(new SpriteBatch(device));
        }



        internal void Begin(SpriteSortMode spriteSortMode, BlendState blendState)
        {
            if (reserveBatches.Count > 0)
            {
                batches.Push(reserveBatches.Pop());
                CurrentBatch.Begin(spriteSortMode, blendState);
            }
            else
            {
                batches.Push(new SpriteBatch(device));
                CurrentBatch.Begin(spriteSortMode, blendState);
            }

        }

        internal void End()
        {
            CurrentBatch.End();
            reserveBatches.Push(batches.Pop());
        }
    }
}
